package com.lmpop.frame.servelet;

import com.lmpop.frame.dao.AjaxDbmImpl;
import com.lmpop.trade.ti_newscomment.Ti_newscomment;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

public class AjaxDbServelet extends HttpServlet
{
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
    doPost(req, resp);
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
    System.out.println("servelet");
    resp.setCharacterEncoding("utf-8");
    req.setCharacterEncoding("utf-8");

    List<Ti_newscomment> list = new ArrayList<Ti_newscomment>();

    String news_id = req.getParameter("news_id");
    System.out.println(news_id);
    String sql = "select content,in_date from ti_newscomment where news_id='" + news_id + "'" + "order by in_date desc limit 0,5";
    System.out.println(sql);
    AjaxDbmImpl ad = new AjaxDbmImpl();
    try
    {
      list = ad.getList(sql);

      System.out.println(list); } catch (Exception e) {
      e.printStackTrace();
    }JSONArray results = new JSONArray();
    if (list != null) {
      System.out.println(list);
      for (Ti_newscomment comment : list) {
        results.put(new JSONObject(comment));
      }
      resp.setContentType("text/plain");
      PrintWriter pw = resp.getWriter();
      pw.write(results.toString());
    }
  }
}